﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.School;
using SIMS.Global;
using SIMS.Company;
using System.Text;

public partial class School_CourseSubject_CreateEdit : System.Web.UI.Page
{
    private void BindSubject()
    {
        ddlSubjectList.Items.Clear();
        ddlSubjectList.Items.Add(new ListItem("Select", "-1"));

        SubjectList _SubjectList = new SubjectList();
        SubjectDB _SubjectDB = new SubjectDB();
        _SubjectList = _SubjectDB.GetSubjectList(Convert.ToInt32(hidCompanyId.Value));
        if (_SubjectList != null && _SubjectList.Count > 0)
        {
            foreach (Subject _cd in _SubjectList)
            {
                ddlSubjectList.Items.Add(new ListItem(_cd.Name, _cd.Id.ToString()));
            }
        }
    }

    private void BindCourseDetail()
    {
        ddlCourseDetailList.Items.Clear();
        ddlCourseDetailList.Items.Add(new ListItem("Select", "-1"));

        CourseDetailList _courseDetailList = new CourseDetailList();
        CourseDetailDB _courseDetailDB = new CourseDetailDB();
        _courseDetailList = _courseDetailDB.GetCourseDetailList(Convert.ToInt32(hidCompanyId.Value));
        if (_courseDetailList != null && _courseDetailList.Count > 0)
        {
            foreach (CourseDetail _cd in _courseDetailList)
            {
                ddlCourseDetailList.Items.Add(new ListItem(_cd.Name, _cd.Id.ToString()));
            }
        }
    }

    private void ShowCourseDetail()
    {
        CourseDetail _courseDetail = new CourseDetail();
        CourseDetailDB _courseDetailDB = new CourseDetailDB();
        _courseDetail = _courseDetailDB.GetCourseDetail(Convert.ToInt32(ddlCourseDetailList.Text));

        if (_courseDetail != null && _courseDetail.Id > 0)
        {
            StringBuilder _strCourseDetail = new StringBuilder();
            _strCourseDetail.Append("<table cellpadding='3' cellspacing='4'>");
            _strCourseDetail.Append("<tr><td>Course:</td><td>" + _courseDetail.CourseName + "</td></tr>");
            _strCourseDetail.Append("<tr><td>Standard Level:</td><td>" + _courseDetail.StandardLevelName + "</td></tr>");
            _strCourseDetail.Append("<tr><td>Major Field:</td><td>" + _courseDetail.MajorFieldName + "</td></tr>");
            _strCourseDetail.Append("</table>");

            lblCourseDetail.Text = _strCourseDetail.ToString();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[Global.CompanyId] != null)
            {
                hidCompanyId.Value = Session[Global.CompanyId].ToString();
                lblCompany.Text = Session[Global.CompanyName].ToString();

                BindCourseDetail();
                BindSubject();

                if (Request.Params["id"] != null)
                {
                    int _CourseSubjectId = 0;
                    if (int.TryParse(Request.Params["id"].ToString(), out _CourseSubjectId) && _CourseSubjectId > 0)
                    {
                        CourseSubject _CourseSubject = new CourseSubject();
                        CourseSubjectDB _CourseSubjectDB = new CourseSubjectDB();
                        _CourseSubject = _CourseSubjectDB.GetCourseSubject(_CourseSubjectId);

                        if (_CourseSubject.Id > 0)
                        {
                            if (_CourseSubject.Error.ErrorNo > 0)
                            {
                                lblMessage.Text = _CourseSubject.Error.ErrorMessage;
                            }
                            else
                            {
                                hidCourseSubjectId.Value = _CourseSubjectId.ToString();
                                txtDescription.Text = _CourseSubject.Description;
                                ddlCourseDetailList.Text = _CourseSubject.CourseDetailId.ToString();
                                ddlCourseDetailList_SelectedIndexChanged(sender, e);
                                ddlSubjectList.Text = _CourseSubject.SubjectId.ToString();
                                
                            }
                        }
                    }
                    else
                    {
                        lblMessage.Text = "Invalid parameter!";
                    }
                }
            }
            else
            {
                lblMessage.Text = "No company found!";
                btnSubmit.Enabled = false;
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                CourseSubject CourseSubject = new CourseSubject();
                CourseSubjectDB _CourseSubjectDB = new CourseSubjectDB();
                int _CourseSubjectId = 0;

                if (int.TryParse(hidCourseSubjectId.Value, out _CourseSubjectId) && _CourseSubjectId > 0)
                {
                    CourseSubject = _CourseSubjectDB.GetCourseSubject(_CourseSubjectId);
                }
                else
                {
                    CourseSubject.Id = 0;
                    CourseSubject.Error = new MyError();
                }

                if (txtDescription.Text.Trim().Length > 0)
                {
                    CourseSubject.Description = txtDescription.Text;
                }
                else
                {
                    CourseSubject.Description = string.Empty;
                }

                CourseSubject.CourseDetailId = Convert.ToInt32(ddlCourseDetailList.Text);
                CourseSubject.SubjectId = Convert.ToInt32(ddlSubjectList.Text);
                CourseSubject.IsActive = false;

                _CourseSubjectDB.Save(CourseSubject);

                if (CourseSubject.Error.ErrorNo > 0)
                {
                    if (CourseSubject.Error.ErrorNo == 1)
                    {
                        lblMessage.Text = CourseSubject.Error.ErrorMessage;
                    }
                    else
                    {
                        lblMessage.Text = "(" + CourseSubject.Error.ErrorLocation + ")";
                        lblMessage.Text += CourseSubject.Error.ErrorMessage;
                        lblMessage.Text += "<br/><br/>" + CourseSubject.Error.ErrorStack;
                    }
                }
                else
                {
                    lblMessage.Text = "Saved successfully!";
                    Response.Redirect("default.aspx?companyid=" + hidCompanyId.Value);
                }
            }
            else
            {
                lblMessage.Text = "Invalid form.";
            }
        }
        catch (Exception Ex)
        {
            lblMessage.Text = Ex.Message;
            MyError.CreateExceptionLog(Ex, "Company_CreateEdit");
        }

    }

    protected void ddlCourseDetailList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCourseDetailList.Items.Count > 0 && ddlCourseDetailList.SelectedIndex > 0)
        {
            ShowCourseDetail();
        }
        else
        {
            lblCourseDetail.Text = "Please select course detail.";
        }
    }
}